package com.mcxx.modules.miniSalvation.fundpay.service.read;

import com.mcxx.common.exception.BizException;
import com.mcxx.constant.DictConstant;
import com.mcxx.modules.base.service.read.DictionaryReadService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.fundpay.dao.read.SingleFundPayReadDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.*;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.SingleFundPayMemberEntity;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayFamilyParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayListSearchParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayParam;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.module.dict.entity.TaDictPo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("singleFundPayReadService")
@TaTransactional(readOnly = true)
public class SingleFundPayReadService extends BaseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SingleFundPayReadService.class);

    @Autowired
    private SingleFundPayReadDao singleFundPayReadDao;
    @Autowired
    private DictionaryReadService dictionaryReadService;


    public Page getFundPayList(FundPayListSearchParam fundPayListSearchParam, PageParam pageParam, UserAccountVo userAccountVo) {
        if (StringUtils.isBlank(fundPayListSearchParam.getPayDate())) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM");
            LocalDate localDate = LocalDate.now();
            String s1 = df.format(localDate);
            fundPayListSearchParam.setPayDate(s1 + "-01");
        }

        QueryParamUtil.setBaseParamValue(fundPayListSearchParam, userAccountVo);


        singleFundPayReadDao.beginPager(pageParam);
        List<FundPayEntity> fundPayEntityList = singleFundPayReadDao.getFundPayList(fundPayListSearchParam);

        return singleFundPayReadDao.endPager(fundPayEntityList);
    }


    public void verifyHasOrderData(CapitalFundPayDTO capitalFundPayDTO, UserAccountVo userAccountVo) {

        FundPayDTO fundPayDTO = new FundPayDTO();
        BeanUtils.copyProperties(capitalFundPayDTO, fundPayDTO);
        QueryParamUtil.setBaseParamValue(fundPayDTO, userAccountVo);

        int rst = singleFundPayReadDao.cntFamilyByCapitalAndAllowance(fundPayDTO);
        if (rst <= 0) {

            throw new BizException("-1", "没有可以添加到名册的对象，无法生成名册");
        }
    }


    public List<String> verifyHasCreateStatRoll(SingleRollDTO params) {
        List<String> row = singleFundPayReadDao.countStatRoll(params);
        return row;
    }


    public List<SingleFundPayMemberEntity> getCapitalStatRollDetail(SingleRollDTO capitalFundPayDTO) {

        return singleFundPayReadDao.selectCapitalStatRollDetail(capitalFundPayDTO.getStatRollId(), capitalFundPayDTO.getBusinessType());



//            List<SingleFundPayMemberEntity> list = singleFundPayReadDao.selectCapitalStatRollDetail(capitalFundPayDTO.getStatRollId(), capitalFundPayDTO.getBusinessType());




    }


    public Map<String, List<String>> getIdByIdAndStates(List<String> fundPayIdList, List<String> stateList) {
        Map<String, List<String>> rstMap = new HashMap<>();

        List<String> canDelIdList = singleFundPayReadDao.getIdByIdsAndStates(fundPayIdList, stateList);
        if (canDelIdList.size() == fundPayIdList.size()) {

            rstMap.put("success", canDelIdList);
            return rstMap;
        }
        if (canDelIdList.size() == 0) {

            rstMap.put("fail", fundPayIdList);
            return rstMap;
        }

        List<String> canNotDelIdList = fundPayIdList.stream().filter(id -> !canDelIdList.contains(id)).collect(Collectors.toList());
        rstMap.put("success", canDelIdList);
        rstMap.put("fail", canNotDelIdList);
        return rstMap;
    }


    public FundPayEntity isCanOperate(String fundPayId, List<String> stateList) {
        FundPayEntity fundPayEntity = singleFundPayReadDao.getById(fundPayId);
        if (!stateList.contains(fundPayEntity.getStatus())) {
            TaDictPo taDictPo = dictionaryReadService.getCodeCache(DictConstant.PAYMENT_MANNER, fundPayEntity.getStatus());
            throw new BizException("-1", "当前名册状态为[" + taDictPo.getLabel() + "]无法执行此操作");
        }
        return fundPayEntity;
    }


    public Page getFundPayFamilyList(SingleRollDTO fundPayFamilyParam, PageParam pageParam, UserAccountVo curUserAccount) {
        QueryParamUtil.setBaseParamValue(fundPayFamilyParam, curUserAccount);
        singleFundPayReadDao.beginPager(pageParam);
        List<SingleFundPayMemberEntity> list = singleFundPayReadDao.selectRollDetail(fundPayFamilyParam);
        return singleFundPayReadDao.endPager(list);
    }

    public Map<String, List<String>> getIdByIdAndState(List<String> fundPayIdList, String confirm) {
        Map<String, List<String>> rstMap = new HashMap<>();

        List<String> successIdList = singleFundPayReadDao.getIdByIdsAndState(fundPayIdList, confirm);
        if (successIdList.size() == fundPayIdList.size()) {

            rstMap.put("success", successIdList);
            return rstMap;
        }
        if (successIdList.size() == 0) {

            rstMap.put("fail", fundPayIdList);
            return rstMap;
        }

        List<String> failIdList = fundPayIdList.stream().filter(id -> !successIdList.contains(id)).collect(Collectors.toList());
        rstMap.put("success", successIdList);
        rstMap.put("fail", failIdList);
        return rstMap;
    }



    public void isExistTargetFundPay(SingleRollDTO singleRollDTO) {
        if (StringUtils.isEmpty(singleRollDTO.getQueryType())) {
            singleRollDTO.setQueryType("0");
        }
        String rst = singleFundPayReadDao.cntFundPay(singleRollDTO);
        if (StringUtils.isNotBlank(rst)) {

            throw new BizException(FailureCode.ERR_10044);
        }
    }


    public List<SingleFundPayMemberEntity> selectStandRollDetail(SingleRollDTO singleRollDTO) {
        return singleFundPayReadDao.selectRollDetail(singleRollDTO);
    }


    public List<FundPayExcelDto> exportFundPayList(FundPayParam param) {
        return singleFundPayReadDao.exportFundPayList(param);
    }

    public List<FundPayFamilyExcelDTO> exportFundPayRollDetail(FundPayFamilyParam param) {
        return singleFundPayReadDao.exportFundPayRollDetail(param);
    }

    public List<String> verifyFundPayRollIsExist(List<String> fundPayIdList, String state) {
        return singleFundPayReadDao.selectFundPayRollIs(fundPayIdList, state);
    }


    public List<SingleFundPayMemberEntity> getFamilyStatRollDetail(SingleRollDTO singleRollDTO) {

        return singleFundPayReadDao.selectFamilyStatRollDetail(singleRollDTO.getStatRollId(), singleRollDTO.getBusinessType());
    }
}
